package org.inspiration.dudu.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.inspiration.dudu.user.model.entity.UserEntity;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户数据访问层接口，提供对用户信息的操作方法。
 */
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {

    /**
     * 查询所有用户信息
     * @return 用户实体列表
     */
    @Select("select * from user")
    List<UserEntity> getAllUsers();

    /**
     * 根据用户ID查询用户信息
     * @param id 用户ID
     * @return 用户实体
     */
    @Select("select * from user where id = #{id}")
    UserEntity getUserById(Long id);

    /**
     * 根据用户名查询用户信息
     * @param username 用户名
     * @return 用户实体
     */
    @Select("select * from user where username = #{username}")
    UserEntity getUserByUsername(String username);

    /**
     * 创建新用户
     * @param user 用户实体
     */
    @Insert("insert into user(username, password) values (#{username}, #{password})")
    void createUser(UserEntity user);

    /**
     * 根据用户ID更新用户信息
     * @param userEntity 用户实体
     */
    @Update("update user set email = #{email}, full_name = #{fullName}, description = #{description} ,gender = #{gender}, phone_number = #{phoneNumber}, address = #{address}, date_of_birth = #{dateOfBirth} where id = #{id}")
    void updateUserById(UserEntity userEntity);

    /**
     * 上传用户头像
     * @param id 用户ID
     * @param url 头像URL
     */
    @Update("update user set profile_picture = #{url} where id = #{id}")
    void uploadAvatar(Long id, String url);

    /**
     * 增加用户点赞数
     * @param userId 用户ID
     */
    @Update("update user set like_count = like_count + 1 where id = #{userId}")
    void increaseLikeCount(Long userId);

    /**
     * 减少用户点赞数
     * @param userId 用户ID
     */
    @Update("update user set like_count = like_count - 1 where id = #{userId}")
    void decreaseLikeCount(Long userId);

    /**
     * 增加用户关注者数量
     * @param followingId 被关注者ID
     */
    @Update("update user set follower_count = follower_count + 1 where id = #{followingId}")
    void increaseFollowerCount(Long followingId);

    /**
     * 增加用户关注数量
     * @param followerId 关注者ID
     */
    @Update("update user set following_count = following_count + 1 where id = #{followerId}")
    void increaseFollowingCount(Long followerId);

    /**
     * 减少用户关注者数量
     * @param followingId 被关注者ID
     */
    @Update("update user set follower_count = follower_count - 1 where id = #{followingId}")
    void decreaseFollowerCount(Long followingId);

    /**
     * 减少用户关注数量
     * @param followerId 关注者ID
     */
    @Update("update user set following_count = following_count - 1 where id = #{followerId}")
    void decreaseFollowingCount(Long followerId);

    /**
     * 根据用户名获取用户ID
     * @param username 用户名
     * @return 用户ID
     */
    @Select("select id from user where username = #{username}")
    Long getUserIdByUsername(String username);

    /**
     * 增加用户文章数量
     * @param userId 用户ID
     */
    @Update("update user set article_count = article_count + 1 where id = #{userId}")
    void increaseArticleCount(Long userId);

    /**
     * 增加用户提问数量
     * @param userId 用户ID
     */
    @Update("update user set question_count = question_count + 1 where id = #{userId}")
    void increaseQuestionCount(Long userId);

    /**
     * 增加用户回答数量
     * @param userId 用户ID
     */
    @Update("update user set answer_count = answer_count + 1 where id = #{userId}")
    void increaseAnswerCount(Long userId);


    @Update("update user set unread_like_count = unread_like_count + 1 where id = #{userId}")
    void increaseUnreadLikeCount(Long userId);

    @Update("update user set unread_like_count = unread_like_count - 1 where id = #{userId}")
    void decreaseUnreadLikeCount(Long userId);

    @Update("update user set unread_comment_count = unread_comment_count + 1 where id = #{repliedUserId}")
    void increaseUnreadCommentCount(Long repliedUserId);

    @Update("update user set unread_comment_count = unread_comment_count - 1 where id = #{repliedUserId}")
    void decreaseUnreadCommentCount(Long repliedUserId);

    @Update("update user set last_active_time = #{lastActiveTime} where id = #{userId}")
    void updateLastActiveTime(Long userId, LocalDateTime lastActiveTime);
}
